﻿using System;
using UnityEngine;
using System.IO;
using System.Xml;
using System.Linq;
using System.Text;
using System.Collections.Generic;



/// <summary>  
/// 地址数据  
/// </summary>  
public class XmlHander
{
    public static string localUrl = Application.dataPath + "/StreamingAssets/";
    private static Dictionary<string, XmlDocument> gameXmlDic = new Dictionary<string, XmlDocument>(); // xml 配置表字典，下标为xml配置表名字。

    /// <summary>  
    /// 加载特定名字的xml文档 
    /// 存储所有配置xml与 xml字典。字典下标为xml名字
    /// 在程序初始化的时候调用。加载所有xml配置表。
    /// </summary>  
    public static void ReadAndLoadXml(string xmlName) // 实现单例 做到全局模块可读取 
    {
        XmlDocument doc = new XmlDocument();
        
        string xmlUrl = localUrl + xmlName + ".xml";

        if (!checkGameDicKey(xmlName))
        {
            doc.Load(xmlUrl);// 判断读取完成 
            gameXmlDic.Add(xmlName, doc);
        }   
    }

    // 查询当前配置表字典是否含有当前项
    private static Boolean checkGameDicKey(string xmlName)
    {
        return gameXmlDic.Keys.Contains<string>(xmlName);
    }

    // 获取配置表
    public static XmlDocument getSetDoc(string xmlName)
    {
        if (!checkGameDicKey(xmlName))
        {
            ReadAndLoadXml(xmlName);
        }
        return gameXmlDic[xmlName];
    }
    /// <summary>
    /// 将字典里面的xml表内容返回
    /// </summary>
    /// <param name="xmlName"></param>
    /// <returns></returns>
    private static XmlDocument getXmlDoc(string xmlName)
    {
        XmlDocument retDoc = getSetDoc(xmlName);// 获取指定的xml表s
        return retDoc;
    }
    /// <summary>
    /// 返回子节点里面的全部内容
    /// </summary>
    /// <param name="node"></param>
    /// <returns></returns>
    public static XmlNodeList getChildNodes(XmlNode node) //List<string> getNodeList(string xmlName, string nodeName)
    {
        return node.ChildNodes;
    }
    /// <summary>
    /// 根据父节点，获取子节点
    /// </summary>
    /// <param name="xmlName"></param>
    /// <param name="nodeName"></param>
    /// <returns></returns>
    public static XmlNode getRootNode(string xmlName, string nodeName)
    {
        string name = xmlName;
        XmlDocument xmlDoc = getXmlDoc(name);
        return xmlDoc.SelectSingleNode(nodeName);
    }
    /// <summary>
    /// 根据子节点，获取二级子节点
    /// </summary>
    /// <param name="xn"></param>
    /// <param name="paramName"></param>
    /// <returns></returns>
    public static XmlNodeList getChildNodeList(XmlNode xn, string paramName) 
    {
        XmlNodeList retNodeList = xn.SelectNodes(paramName);
        return retNodeList;
    }
    /// <summary>
    /// 根据父节点，返回子节点
    /// </summary>
    /// <param name="xn"></param>
    /// <param name="paramName"></param>
    /// <returns></returns>
    public static XmlNode getChildNode(XmlNode xn, string paramName)
    {
        XmlNode retNode = xn.SelectSingleNode(paramName);
        return retNode;

    }
    /// <summary>
    /// 将二级子节点拆成逐句读取
    /// </summary>
    /// <param name="xn"></param>
    /// <param name="strId"></param>
    /// <returns></returns>
    public static List<string> getNodeInnerTxt(XmlNodeList nodeList)//, string strId) //  拆成逐句读取
    {
        List<string> nowAllContent = new List<string>();

        for (int i = 0; i < nodeList.Count; i++)
        {
           nowAllContent.Add(nodeList.Item(i).InnerText);
        }
        return nowAllContent;
    }


    /// <summary>
    /// 获取指定nodeList 下的某个attribute指，拼接成string返回
    /// </summary>
    /// <param name="nodeList"></param>
    /// <param name="attributeName"></param>
    /// <returns></returns>
    public static List<string> getNodeListAttribute(XmlNodeList nodeList, string attributeName)
    {
        List<string> nowAllContent = new List<string>();


         foreach (XmlNode nodeAttribute in nodeList)
         {
             XmlElement _nodeAttribute = (XmlElement)nodeAttribute;

             nowAllContent.Add(_nodeAttribute.GetAttribute(attributeName));
         }

        return nowAllContent;
    }



}

